package com.example.studyenglish.activity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.example.studyenglish.R;
import com.example.studyenglish.database.DataAccess;
import com.example.studyenglish.pattern.Vocabulary;

import java.util.ArrayList;


public class Rusty extends ListActivity {
	private ArrayList<Vocabulary> list;
	public static final int MENU_ADD = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setTitle("生疏词汇");
		this.setContentView(R.layout.rusty);
		setAdapter();
	}

	@Override
	protected void onListItemClick(ListView l, View v, final int position,
                                   long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		Dialog dialog = new AlertDialog.Builder(Rusty.this)
				.setTitle("操作")
				.setItems(new String[] { "从生疏词汇库中删除" },
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
                                                int which) {

								DataAccess data = new DataAccess(Rusty.this);
								data.DeleteFromAttention(list.get(position));
								setAdapter();

							}

						})
				.setNegativeButton("取消", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub

					}
				}).create();

		dialog.show();
	}

	protected void setAdapter() {
		// TODO Auto-generated method stub
		DataAccess data = new DataAccess(this);
		list = data.QueryAttention(null, null);
		ArrayList<String> showlist = new ArrayList<String>();
		for (int i = 0; i < list.size(); i++) {
			showlist.add((i + 1) + "." + list.get(i).getSpelling() + "\n"
					+ list.get(i).getMeanning());
		}
		this.setListAdapter(new ArrayAdapter<String>(this, R.layout.file_row,
				showlist));
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		this.setAdapter();
		super.onResume();
	}

}
